import { CoreConfigPropertyOption } from './decorator/core-config-property.decorator'

export class CoreConfigFactory {
  /**
   * 配置属性
   * 配置实体名 - 属性集合
   */
  protected static configPropertyMap = new Map<string, string[]>()

  /**
   * 注册配置属性
   * @param target
   * @param propertyKey
   * @param option
   */
  static registryProperty(target: Object, propertyKey: string, option?: CoreConfigPropertyOption) {
    const targetName = target.constructor.name

    propertyKey = option?.property ?? propertyKey

    if (!this.configPropertyMap.has(targetName)) {
      this.configPropertyMap.set(targetName, [propertyKey])
    } else {
      const properties = this.configPropertyMap.get(targetName)
      if (!properties.includes(propertyKey)) {
        properties.push(propertyKey)
      }
    }
  }

  /**
   * 获取配置属性
   * @param target
   * @returns
   */
  static getConfigProperties(target: Function | Object): string[] {
    const targetName = typeof target === 'function' ? target.name : target.constructor.name
    return this.configPropertyMap.get(targetName) || []
  }
}
